package org.csu.mypetstorebackstage.controller;

//import com.sun.org.apache.xpath.internal.operations.Mod;
import org.csu.mypetstorebackstage.domain.Item;
import org.csu.mypetstorebackstage.domain.Product;
import org.csu.mypetstorebackstage.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.mail.Session;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @GetMapping("/getProductView")
    public String getProductView(Model model,String categoryName,HttpSession session)
    {
        System.out.println("传入product控制器的category是"+categoryName);
        List<Product> productList = productService.getProductAllByCategory(categoryName);
        System.out.println("传入product控制器的List是"+productList);
        session.setAttribute("productList",productList);
        session.setAttribute("categoryNameTest",categoryName);
        return "category/product";
    }

    @PostMapping("/getSpecialProduct")
    public String getSpecialProduct(Model model,String productName,String categoryName)
    {
        if(productName.equals("input the name of product to search"))
        {
            List<Product> productList = productService.getProductAllByCategory(categoryName);
            System.out.println(productList);
            model.addAttribute("productList",productList);
            return "category/product";
        }
        else
        {
            Product product = productService.getProductTest(productName);
            List<Product> productList=new ArrayList<>();
            productList.add(product);
            model.addAttribute("productList",productList);
            System.out.println(product.getCategoryId());
            return "category/product";
        }
    }

    @GetMapping("/toaddProduct")
    public String toaddItem()
    {
        return "/category/AddProduct";
    }
    @PostMapping("/addProduct")

    public String addProduct(Product product, Model model, HttpSession session)
    {
        Boolean isInsert=productService.addProduct(product);
        if(isInsert==true)
        {
            model.addAttribute("Message","已加入成功");
            List<Product> productList=productService.getProductListByCategory(product.getCategoryId());
            session.setAttribute("productList",productList);
            return "/category/product";
        }
        else
        {
            model.addAttribute("Message","Exist");
            return "/category/AddProduct";
        }
    }

    @GetMapping("/deleteProduct")
    public String deleteProduct(String productId ,Product product,HttpSession session,Model model,String categoryName,int delete)
    {
        System.out.println("删除界面返回的category是"+categoryName);
//        product=productService.getProduct(categoryName);;
//        System.out.println(product);
//        String categoryName1=product.getCategoryId();
//        System.out.println(categoryName1);
//        productService.deleteProduct(product.getProductId());

//        List<Product>productList=productService.getProductAll();
        if(delete==3)
        {

            System.out.println(delete);
            System.out.println("delete 后面 productId"+product.getProductId());
            productService.deleteProduct(productId);
            System.out.println("hahahha");
        }
        List<Product>productList=productService.getProductAllByCategory(categoryName);
        Iterator<Product>productIterator=productList.listIterator();
        while(productIterator.hasNext())
        {
            System.out.println(productIterator.next().getName());
        }
        session.setAttribute("productList",productList);
        session.setAttribute("categoryName",categoryName);
        return "/category/product";
    }


}
